//
// Created by xbm on 23-3-9.
//

#ifndef WEBSERVER_CONCURRENTQUEUE_H
#define WEBSERVER_CONCURRENTQUEUE_H
#include <queue>
#include <mutex>
#include <optional>

using namespace std;

namespace util {
template<typename T>
class ConcurrentQueue {
public:
    optional <T> pop() {
        lock_guard lg(mx_);
        if (qu_.empty()) return nullopt;
        T ret = std::move(qu_.front());
        qu_.pop();
        return std::move(ret);
    }

    void push(const T &v) {
        lock_guard lg(mx_);
        qu_.push(v);
    }

    void push(T &&v) {
        lock_guard lg(mx_);
        qu_.push(std::move(v));
    }

    bool empty() {
        return qu_.empty();
    }

    int size() {
        return qu_.size();
    }

private:
    queue <T> qu_;
    mutex mx_;
};
}
#endif //WEBSERVER_CONCURRENTQUEUE_H
